package thread;

/**
 * 静态方法上如果使用了synchronized，那么该方法一定具备同步效果
 */
public class SyncDemo3 {
    public static void main(String[] args) {
        sho f1 = new sho();
        sho f2 = new sho();
        Thread t1 = new Thread("idh") {
            public void run() {
                f1.buy();
            }
        };
        Thread t2 = new Thread("rgbig") {
            public void run() {
                f2.buy();
            }
        };
        t1.start();
        t2.start();
    }
}

/*
静态方法中如果使用同步块时，指定锁对象通常也用该对象，那么引用类对象的格式可以为：类名.class

 */

class sho {
    public synchronized static void buy() {
        synchronized (sho.class) {
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName() + "正在挑衣服");
                Thread.sleep(5000);
                System.out.println(t.getName() + "正在试衣服");
                Thread.sleep(5000);
                System.out.println(t.getName() + "买完了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
